package com.atguigu.test

import java.text.SimpleDateFormat
import scala.io.Source

/*
需求: 统计每个区域的平均等客时间

 */
object Test1 {
  def main(args: Array[String]): Unit = {
    //读取文件
    val list = Source.fromFile("datas//data.txt").getLines().toList
    //1.列裁剪
    list.map(line=> {
      val arr = line.split(",")
      val user = arr(0)
      val fromaddr = arr(1)
      val toaddr = arr(2)
      val format = new SimpleDateFormat("yyyy-MM-dd HH:mm-ss")
      val fromtime = arr(3)
      val fromTime = format.parse(fromtime).getTime
      val totime = arr(4)
      val toTime = format.parse(totime).getTime
      (user,fromaddr,toaddr,fromTime,toTime)
    })
    //2.按照司机分组
      .groupBy{
        case (user,fromaddr,toaddr,fromtime,totime)=>{
          user
        }
      }
        /*
        map{
          a->List((A	龙华区	宝安区	2020-07-15 10:05:10	2020-07-15 10:25:02),
                  (A	宝安区	龙岗区	2020-07-15 10:35:15	2020-07-15 10:40:50)),
          b->List( (B	宝安区	福田区	2020-07-15 11:43:22	2020-07-15 11:55:45)
        }
         */
    //3.每个司机的接客时间排序
      .map{
        case (user,list)=>
          val sortlist = list.sortBy {
            case (user, fromaddr, toaddr, fromtime, totime) => totime
          }

          val slidinglist = sortlist.sliding(2)
          //List(
          //  List((A	龙华区	宝安区	2020-07-15 10:05:10	2020-07-15 10:25:02),
          //        (A	宝安区	龙岗区	2020-07-15 10:35:15	2020-07-15 10:40:50)
          //窗口里面两个元组计算
          val regionlist = slidinglist.map(list=> {
            val first = list.head
            val second = list.last
            //等车区域
            val waitaddr = first._3
            //等车时间
            val waittime = (second._4-first._5)/1000
            (waitaddr,waittime)
          }
          )
          regionlist
      }.foreach(println)

  }
}
